package com.gps.skyrc.activity;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.scan.BleScanRuleConfig;
import com.gps.skyrc.R;
import com.gps.skyrc.activity.base.BaseActivity;
import com.gps.skyrc.api.Config;
import com.gps.skyrc.bean.DeviceInfo;
import com.gps.skyrc.tool.SharedPreferencesUtils;
import com.gps.skyrc.tool.Tip;
import com.gps.skyrc.tool.logger.ILogger;

import org.litepal.LitePal;

import java.util.List;
import java.util.logging.Handler;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class SplashActivity extends BaseActivity {

    private List<DeviceInfo> mDeviceInfos;

    @Override
    protected void getLayout() {
        setContentView(R.layout.splash_activity);
    }

    @Override
    protected void setTitleBar() {

    }

    @Override
    protected void init() {
        mDeviceInfos = LitePal.findAll(DeviceInfo.class);
        String mac = (String) SharedPreferencesUtils.getParam(mContext, SharedPreferencesUtils.PreKey.LAST_DEVICE_MAC, "");
        assert mac != null;
        //只是检测权限是否开启，不会搜索  不会连接
        //不论是否有无数据  均跳主页
        checkPermission();
    }

    private void checkPermission() {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!bluetoothAdapter.isEnabled()) {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, 0x01);
            return;
        }
        SplashActivityPermissionsDispatcher.showLocationWithPermissionCheck(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x01) {
            if (resultCode == Activity.RESULT_OK) {
                SplashActivityPermissionsDispatcher.showLocationWithPermissionCheck(this);
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Tip.showToast(mContext, R.string.open_ble);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        SplashActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @NeedsPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
    void showLocation() {//获得权限成功后
        new android.os.Handler().postDelayed(() -> {
            startActivity(MainActivity.class);
            defaultFinish();
        },3000);
    }

    @OnShowRationale(Manifest.permission.ACCESS_COARSE_LOCATION)
    void showRationaleForExternal(final PermissionRequest request) {
        //注册一些解释为什么需要权限的方法。
        Tip.showToast(mContext, R.string.permission_rationale);
        request.proceed();
    }

    @OnPermissionDenied(Manifest.permission.ACCESS_COARSE_LOCATION)
    void showDeniedForExternal() {
        //注册一些需要权限的方法。
        Tip.showToast(this, R.string.permission_rationale);
    }

    @OnNeverAskAgain(Manifest.permission.ACCESS_COARSE_LOCATION)
    void showNeverAskForExternal() {
        //注册一些处理用户选择的方法以永久拒绝权限。
        Tip.showToast(this, R.string.permission_never_ask_again);
    }

}
